package com.starry.home.ui.mine

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.View.VISIBLE
import android.widget.EditText
import com.starry.core.base.BaseActivity
import com.starry.core.ext.singleClick
import com.starry.core.ext.toast
import com.starry.home.R
import com.starry.home.databinding.ActivityBmiBinding

/**
 * Package:com.starry.home.ui.mine
 * Author:starr
 * Time:2024/4/29 14:29
 * Description: BMI计算器
 */
class BMIActivity : BaseActivity<ActivityBmiBinding, EmptyViewModel>() {


    private var sex = 0
    private var height: Float = 0f
    private var heavy: Float = 0f

    private lateinit var editHeight: EditText
    private lateinit var editHeavy: EditText

    companion object {
        fun startAction(context: Context) {
            val intent = Intent(context, BMIActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun initData() {

        mDataBinding.llMale.setOnClickListener {
            sex = 1
            mDataBinding.llMale.setBackgroundResource(R.drawable.shape_register_sex_select)
            mDataBinding.llGirl.setBackgroundResource(R.drawable.shape_register_sex_normal)
        }

        mDataBinding.llGirl.setOnClickListener {
            sex = 2
            mDataBinding.llMale.setBackgroundResource(R.drawable.shape_register_sex_normal)
            mDataBinding.llGirl.setBackgroundResource(R.drawable.shape_register_sex_select)
        }

        mDataBinding.tvCalculate.singleClick {
            if (sex == 0) {
                toast("请选择性别")
                return@singleClick
            }
            if (editHeavy.text.isNullOrEmpty()) {
                toast("请输入体重")
                return@singleClick
            }
            if (editHeight.text.isNullOrEmpty()) {
                toast("请输入身高")
                return@singleClick
            }
            height = mDataBinding.editHeight.text.toString().toFloat() / 100
            heavy = mDataBinding.editHeavy.text.toString().toFloat()
            val bmi = heavy / (height * height)
            mDataBinding.tvBmi.text = String.format("%.2f", bmi)
            toast("您的BMI指数为${String.format("%.2f", bmi)}")
            when (bmi) {
                in 0.0..18.4 -> {
                    mDataBinding.tvBmiStatus.text = "偏瘦"
                }

                in 18.5..23.9 -> {
                    mDataBinding.tvBmiStatus.text = "正常"
                }

                in 24.0..27.9 -> {
                    mDataBinding.tvBmiStatus.text = "过重"
                }

                else -> {
                    mDataBinding.tvBmiStatus.text = "肥胖"
                }
            }
        }

    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        editHeavy = mDataBinding.editHeavy
        editHeight = mDataBinding.editHeight
        mDataBinding.titleTopBar.baseTvTitle.text = "BMI计算器"
        mDataBinding.titleTopBar.baseIvBack.setOnClickListener {
            finish()
        }
        mDataBinding.titleTopBar.baseIvBack.visibility = VISIBLE
    }

    override fun getLayoutId() = R.layout.activity_bmi
}